<?php
namespace app\Manage\controller;
use app\common\controller\Manage;
use app\common\model\Article as articleModel;
use think\facade\Request;

class Article extends Manage
{
    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $article = new articleModel();
        if(Request::isAjax())
        {
            return $article->tableData(input('param.'));
        }
        $articleTypeModel = new \app\common\model\ArticleType();
        $typeList = $articleTypeModel->getList();
        return $this->fetch('',['typeList' => $typeList]);
    }

    /**
     * 文章添加
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $articleModel = new articleModel();
        if(Request::isPost())
        {
            return $articleModel->addData(input('param.'));
        }
        $articleTypeModel = new \app\common\model\ArticleType();
        $list = $articleTypeModel->getList();
        return $this->fetch('', ['list' => $list]);
    }

    /**
     * 文章编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $article = new articleModel();
        if(Request::isPost())
        {
            return $article->saveData(input('param.'));
        }
        $info = $article->with('articleType')->where('id',input('param.id/d'))->find();
        if (!$info)
        {
            return error_code(10002);
        }
        $articleTypeModel = new \app\common\model\ArticleType();
        $typeList = $articleTypeModel->getList();
        $this->assign('typeList', $typeList);
        return $this->fetch('edit',['info'=>$info]);
    }

    /**
     * 删除文章
     * @return array
     */
    public function del()
    {
        $article = new articleModel();
        if ($article->destroy(input('param.id/d'))) {
            $result = [
                'status' => true,
                'msg' => '删除成功',
                'data' => ''
            ];
        } else {
            $result = [
                'status' => false,
                'msg' => '删除失败',
                'data' => ''
            ];
        }
        return $result;
    }



    /**
     *  更改文章状态
     *
     */
    public function changeState()
    {
        $result = [
            'status' => false,
            'msg' => '关键参数丢失',
            'data' => []
        ];
        $articleModel = new articleModel();
        $id = input('param.id/d', 0);
        $elem = input('param.elem/s', '');
        $state = input('param.state/s', 'true');
        if (!$id && !$elem) return $result;
        $change = $state === 'true'
            ? $articleModel::STATUS_YES
            : $articleModel::STATUS_NO;
        switch ($elem)
        {
            case 'rec':
                $iData['is_rec'] = $change;
                break;
            case 'hot':
                $iData['is_hot'] = $change;
                break;
            case 'pub':
                $iData['is_pub'] = $change;
                break;
            default:
                $iData = '';
                break;
        }

        if ($articleModel->save($iData, ['id' => $id]))
        {
            $result['status'] = true;
            $result['msg'] = '设置成功';
        } else {
            $result['msg'] = '设置失败';
        }

        return $result;
    }
}